<template>
  <component is="a-modal" v-model:visible="visible" :width="600" :title="title" :mask-closable="false"
    :ok-loading="loading" @cancel="close" @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="会员UID" field="user_id">
        <a-input v-model="formData.user_id" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="推广地区" field="can_see_code">
        <a-checkbox v-model="checked1" @change="getchange">地区1</a-checkbox>
        <a-checkbox v-model="checked2">地区2</a-checkbox>
        <a-checkbox v-model="checked3">地区3</a-checkbox>
      </a-form-item>
      <a-form-item label="操作密码" field="admin_pay_password">
        <a-input-password v-model="formData.admin_pay_password" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="谷歌验证码" field="google_code">
        <a-input v-model="formData.google_code" placeholder="请输入" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed, watch } from 'vue'
import { Message } from '@arco-design/web-vue'
import memberApi from '@/api/system/member'
import ENUMS from '@/utils/enums'
import { throttle } from 'lodash'
const title=ref('')
const paramsData = ref({})
const checked1 = ref(false)
const checked2 = ref(false)
const checked3 = ref(false)
const getParams = async () => {
  const { data } = mode.value=='add'? await memberApi.member_white_add_config():await memberApi.member_white_update_config(formData.id)
  paramsData.value = { ...data }
  checked1.value = data.info?.can_see_code[0] == 1 ? true : false
  checked2.value=data.info?.can_see_code[1]==1?true:false
  checked3.value=data.info?.can_see_code[2]==1?true:false
  
}
const AREA = [
  { title: '地区1', value: 1 },
  { title: '地区2', value: 1 },
  {title:'地区3',value:1},
]
const emit = defineEmits(['success'])
// 引用定义
const formRef = ref()
const mode = ref('')
const visible = ref(false)
const loading = ref(false)
const roleData = ref([])

// 表单信息
const formData = reactive({
  user_id: '',
  admin_pay_password: '',
  google_code: '',
  can_see_code:[],
  id: ''
})

// 验证规则
const rules = ref({
  user_id: [{ required: true, message: '请输入' }],
  admin_pay_password: [{ required: true, message: '请输入' }],
  google_code: [{ required: true, message: '请输入' }],
  can_see_code: [{ required: true, message: '请选择' }],
});
// 打开弹框
const open = async (type = 'add',data={}) => {
  if (type == 'add') {
    title.value = '添加白名单'
  } else { 
    title.value = '编辑白名单'
    formData.id=data.id
  }
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await getParams()
}
const getchange = async (e) => {
  console.log(11,e);
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
  console.log('formData', formData);
}

// 数据保存
const submit = throttle(async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    try {
      let data = { ...formData }
      let result = {}
      if (mode.value === 'add') {
        // 添加数据
        data.id = ''
        result = await memberApi.member_white_add(data)
      } else {
        checked1.value == true? data.can_see_code[0] = 1 : data.can_see_code[0] = ''
        checked2.value == true ? data.can_see_code[1] = 1 : data.can_see_code[1] = ''
        checked3.value == true ? data.can_see_code[2] = 1 : data.can_see_code[2] = ''
        result = await memberApi.member_white_add(data)
      }
      if (result.code === 200) {
        Message.success('操作成功')
        emit('success')
        done(true)
      }
      // 防止连续点击提交
      setTimeout(() => {
        loading.value = false
      }, 500)
    } catch {
      loading.value = false
    }
  }
  done(false)
}, 100)

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
